package com.ulook.lookplanet.controller;

import com.ulook.lookplanet.bean.TCommunity;
import com.ulook.lookplanet.bean.TTopic;
import com.ulook.lookplanet.bean.TUser;
import com.ulook.lookplanet.bean.VArticlePreInfo;
import com.ulook.lookplanet.common.ExceptionCode;
import com.ulook.lookplanet.common.JsonResult;
import com.ulook.lookplanet.service.Manage2Service;
import com.ulook.lookplanet.service.ManageService;
import com.ulook.lookplanet.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 描述：
 *
 * @author L1998
 * @date 2021/5/13
 */
@RestController
@RequestMapping("/manage")
public class ManageController {

    @Autowired
    private ManageService service;

    /*@Autowired
    private Manage2Service service;*/

    /**
     * 获取基本信息
     */
    @PostMapping("/getTUserById")
    public JsonResult<TUser> getTUserById(HttpServletRequest request){
        String id = (request.getParameter("id"));
        if(id == null || "".equals(id)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数不存在");
        }
        TUser user = service.getTUserById(Integer.parseInt(id));
        return new JsonResult<>(user);
    }

    /**
     * 冻结
     */
    @PostMapping("/freeze")
    public JsonResult freeze(HttpServletRequest request){
        String id = (request.getParameter("id"));
        String state = (request.getParameter("state"));
        if(StringUtil.isEmpty(id) || StringUtil.isEmpty(state)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数不存在");
        }
        service.freeze(Integer.parseInt(id), Integer.parseInt(state));
        return new JsonResult();
    }

    /**
     * 删除账户
     */
    @PostMapping("/deleteUser")
    public JsonResult deleteUser(HttpServletRequest request){
        String id = (request.getParameter("id"));
        if(StringUtil.isEmpty(id)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数不存在");
        }
        service.deleteUser(Integer.parseInt(id));
        return new JsonResult();
    }

    /**
     * 搜索用户
     */
    @PostMapping("/searchUser")
    public JsonResult<List<TUser>> searchUser(HttpServletRequest request){
        String key1 = request.getParameter("key");
        if(StringUtil.isEmpty(key1)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数不存在");
        }
        String key2 = "%" + key1 + "%";
        List<TUser> users = service.searchUser(key1, key2);
        return new JsonResult<>(users);
    }

    /**
     * 获取动态
     */
    @PostMapping("/getSquareArticle")
    public JsonResult<List<VArticlePreInfo>> getSquareArticle(HttpServletRequest request){
        String s = request.getParameter("s");
        if(s == null || "".equals(s)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        String itemNum = request.getParameter("itemNum");
        List<VArticlePreInfo> articlePreInfos = service.searchArticle("%"+s+"%", Integer.parseInt(itemNum));
        if(articlePreInfos.size() == 0){
            return new JsonResult<>(ExceptionCode.NO_DATA, "没有更多数据了");
        }
        return new JsonResult<>(articlePreInfos);
    }

    /**
     * 删除动态
     */
    @PostMapping("/deleteArticle")
    public JsonResult deleteArticle(HttpServletRequest request){
        String uid = request.getParameter("uid");
        if(StringUtil.isEmpty(uid)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        service.deleteArticle(uid);
        return new JsonResult();
    }

    /**
     * 获取话题分组
     */
    @PostMapping("/getComList")
    public JsonResult<List<TCommunity>> getComList(HttpServletRequest request){
        List<TCommunity> communities = service.getComList();
        return new JsonResult<>(communities);
    }

    /**
     * 添加话题
     */
    @PostMapping("/addTopic")
    public JsonResult addTopic(HttpServletRequest request){
        String name = request.getParameter("name");
        if(StringUtil.isEmpty(name)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        String comuid = request.getParameter("comuid");
        TTopic tTopic = new TTopic();
        tTopic.setName(name);
        if(StringUtil.isEmpty(comuid)){
            tTopic.setComuid("0");
        }else{
            tTopic.setComuid(comuid);
        }
        List<MultipartFile> files = ((MultipartHttpServletRequest)request).getFiles("files");
        boolean b = service.addTopic(tTopic, files);
        if(!b){
            return new JsonResult(ExceptionCode.EXIST_DATA, "话题名已存在");
        }
        return new JsonResult();
    }

    /**
     * 删除话题
     */
    @PostMapping("/deleteTopic")
    public JsonResult deleteTopic(HttpServletRequest request){
        String name = request.getParameter("name");
        if(StringUtil.isEmpty(name)){
            return new JsonResult<>(ExceptionCode.NULL_PARAM, "参数为空");
        }
        service.deleteTopic(name);
        return new JsonResult();
    }

}
